package com.test.java01;

import java.util.Random;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        while(true){
            Scanner sc=new Scanner(System.in);
            String str1="";
            char[] arr2=judge_number();//调用方法
            for(int j=0;j< arr2.length;j++) {
                System.out.print(arr2[j]);
                str1=str1+arr2[j];
            }
           System.out.println(" ");
            System.out.println("请输入验证码：");
            String str2=sc.next();
            if(str1.equals(str2)) {
                System.out.println("验证成功！");
                break;
            }
        }


    }


    public static char[] judge_number(){
        char[] arr = new char[4];
        Random r = new Random();
        //先生成字母验证码
        for (int i = 0; i < arr.length - 1; i++) {
            while(true) {
                int num = r.nextInt(58) + 65;
                if (num >= 65 && num <= 90 || num >= 97 && num <= 122) {
                    arr[i] = (char) num;
                    break;
                }
            }
        }
        int m = r.nextInt(10);
        arr[arr.length - 1] = (char) (48 + m);
        //打乱数组索引，从而实现数字的位置随机出现--随机交换索引
        for (int j = 0; j < arr.length; j++) {
            int a = r.nextInt(arr.length);
            int b = r.nextInt(arr.length);
            if (a != b) {
                char temp = arr[a];
                arr[a] = arr[b];
                arr[b] = temp;
            }
        }
        return arr;

    }

}

